package com.codedancer.campus.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.codedancer.campus.client.service.ICategoryService;
import com.codedancer.campus.client.service.vo.category.CategoryVo;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.mapper.CategoryMapper;
import com.codedancer.campus.mapper.PlateMapper;
import com.codedancer.campus.po.Category;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private PlateMapper plateMapper;

    /**
     * 获取某个板块下的所有分类
     * @param plateUuid
     * @return
     */
    @Override
    public List<CategoryVo> getByPlateUuid(String plateUuid) {
        if (!this.plateMapper.existsByUuId(plateUuid)){
            throw new BusinessException("板块不存在");
        }
        List<Category> categories = categoryMapper.selectListByColumn("plate_uuid",plateUuid);
        List<CategoryVo> categoryVos;
        categoryVos = categories.stream().map(category -> BeanUtil.copyProperties(category,CategoryVo.class)).collect(
            Collectors.toList());
        return categoryVos;
    }
}
